#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	75333-nfs.sh
#Version:	1.1
#Date:		2013/10/18
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.04网络服务-05NFS-01字符模式（典型配置）-02添加共享目录-01添加主机为所有主机
#Notes:		NFS服务典型配置
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/11/18
#		-The first one
#-----------------------------------------------------------------------------


#PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
#export PATH

source ../../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../../lib/XmlParse.sh
source ./lib/cmd.sh

function CleanData ()
{
  umount /mnt/rw &> /dev/null
  umount /mnt/ro &> /dev/null
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "command -v systemctl && /bin/systemctl stop nfs.service || /sbin/service nfs stop"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "rm -rf /nfs-test"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo '' >/etc/exports"
  rm -rf /mnt/*
  EchoInfo "75333-nfs.sh执行完毕"
}

trap "CleanData" EXIT INT

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null"
    exit 1
fi

DATA_DIR="${SFROOT}/testcases/Network_Service"
DATA_FILE="config.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

USER=${RUSERNAME}
ServerIP=${REHOST}
PASSWORD=${PASSWORD}

#GetValue USER USER
#GetValue ServerIP ServerIP
#GetValue PASSWORD PASSWORD

#远程配置服务器
EchoInfo "远程配置服务器${ServerIP}"

Scp "${USER}" "${ServerIP}" "${PASSWORD}" "./nfs-config.sh" "/root/"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sh nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo 查看服务器端nfs共享目录清单"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "showmount -e 127.0.0.1"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "rm -rf /root/nfs-config.sh"

#客户端是否可以查看到nfs共享目录
EchoInfo "客户端（本机）开始检验nfs服务器是否配置成功"
stop_daemon iptables  
test -n "`showmount -e ${ServerIP} |grep /nfs-test/readonly`" -a -n "`showmount -e ${ServerIP} |grep /nfs-test/rw`" 
EchoResult "客户端可以看见nfs共享目录"

#客户端在/mnt目录下新建两个目录：rw和ro
test -d /mnt/rw  || mkdir /mnt/rw
test -d /mnt/ro  || mkdir /mnt/ro
#mkdir /mnt/rw /mnt/ro


#客户端挂载只读目录/nfs-test/readonly
mount ${ServerIP}:/nfs-test/readonly /mnt/ro

 
EchoInfo "检验客户端能否在只读目录下进行写操作"
echo "This is a test!" > /mnt/ro/file1
if [ $? -ne 0 ]
then
    echo "Pass|客户端不能在只读目录下进行写操作"
else   
    echo "Fail|客户端不能在只读目录下进行写操作"
fi

#客户端卸载只读目录
EchoInfo "客户端能否成功卸载只读目录"
umount /mnt/ro
#test -z `ls /mnt/ro`
mount | grep /mnt/ro
[ $? -ne 0 ]
EchoResult "客户端成功卸载只读目录"


#客户端挂载读写目录/nfs-test/rw
mount ${ServerIP}:/nfs-test/rw /mnt/rw


EchoInfo "检验客户端能否在读写目录下进行写操作"
echo "This is a test!" > /mnt/rw/file1
EchoResult "客户端在读写目录下进行写操作"

#客户端卸载读写目录
EchoInfo "客户端能否成功卸载读写目录"
umount /mnt/rw
#test -z `ls /mnt/rw`
mount | grep /mnt/rw
[ $? -ne 0 ]
EchoResult "客户端成功卸载读写目录"

